<?php

class CExpress extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;

	protected $from=null;
	
	protected $to_station_id=0;
	
	protected $weight=null;
	
	protected $currency=null;
	
	protected $type=null;
	
	protected $volume=null;
	
	protected $lang='en';
	
	protected $carrier=0;
	
	protected $expressType='';
	
	public function __construct($type){
		
		$this->_primary="expressid";
		
		$this->_name=$type . "_express";
		
		$this->expressType=$type;
		
		parent::__construct();
		
	}
	
	public function setFrom($from){
		
		$this->from=$from;
		
	}
	
	public function setTo($to_station_id){
		
		$this->to_station_id=$to_station_id;
		
	}
	
	public function setWeight($weight){
		
		$this->weight=$weight;
		
	}
	
	public function get_weight(){
		
		return $this->weight;
		
	}
	
	public function setCurrency($currency){
		
		$this->currency=$currency;
		
	}
	
	public function setType($type){
		
		$this->type=$type;
		
	}
	
	public function setVolume($volume){
		
		if($volume==''){
			
			$volume=0;
			
		}
		
		$this->volume=$volume;
		
	}
	
	public function setCarrier($carrier){
		
		$this->carrier=$carrier;
		
	}
	
	public function setLang($lang){
		
		$this->lang=$lang;
		
	}
	
	public function createExpress($data){
		
		$expressid=$this->insert($data);

		return $expressid;
		
	}
	
	public function editExpress($expressid,$data){
		
		$db=$this->getAdapter();
		
		$where = $db->quoteInto('expressid = ?', $expressid);

		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected!=1){
			
			return false;
			
		}else{
			
			return true;
			
		}
		
		
	}
	
	public function getDetail($expressid){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'*');
		
		$select->where($this->_name.'.expressid = ? ',$expressid);
		
		return $db->fetchAll($select);
		
	}
	
	public function delExpress($expressidArray){
		
		$db=$this->getAdapter();
		
		$expressidString=join(',',$expressidArray);
		
		$result=$db->query("DELETE FROM $this->_name WHERE expressid IN ($expressidString)");
		
		return $result;
		
	}
	
	
	
	public function getAllExpressCounts($where=''){
		
		$db=$this->getAdapter();
		
		$select = $db->select();

		$select->from($this->_name,'COUNT(*)');
		
		if($where!=''){
			
			$select->where($where);
	
		}
		
		return $db->fetchOne($select);
		
	}
	
	public function getAllExpress($from,$counts,$where=''){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'*');
		
		if($where!=''){
			
			$select->where($where);
			
		}
		
		$select->joinLeft('express_companies',"$this->_name.carrier=express_companies.companyid",'*');
		
		$select->joinLeft(R_PRE.'locationfls AS A',"$this->_name.to_station_id=A.flid",'A.en_flname AS to_location');

		$select->joinLeft(R_PRE.'locationfls AS B',"$this->_name.from_station_id=B.flid",'B.en_flname AS from_location');
		
		if($this->expressType=='international'){
			
			$select->order("$this->_name.to_station_id ASC");
		
		}else{
			
			$select->order("$this->_name.from_station_en ASC");
			
		}
		
		
		
		if($counts!=0){
		
			$select->limit($counts, $from);
		
		}
		
		return $db->fetchAll($select);
		
	}
	
	public function getShipFrom($lang){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->distinct(true);
		
		$select->from('domestic_express','from_station_'.$lang);
		
		$select->order('from_station_en');
		
		return $db->fetchAll($select);
		
	}
	
	public function getShipTo($lang){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->distinct(true);
		
		$select->from('international_express','to_station_'.$lang);
		
		$select->order('to_station_'.$lang);
		
		return $db->fetchAll($select);
		
	}
	
	public function getCarrier(){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->distinct(true);
		
		$select->from('international_express','carrier');
		
		return $db->fetchAll($select);	
		
	}
	
	public function getWeight(){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->distinct(true);
		
		$select->from($this->expressType.'_express','weight');
		
		$select->where("weight<>''");
		
		$select->order('weight','ASC');
		
		return $db->fetchAll($select);	
		
	}
	
	public function searchInternational(){
		
		if($this->to_station_id&&$this->type!=null&&$this->weight){
			
			$db=$this->getAdapter();
			
			$select = $db->select();
			
			$select->from('international_express','*');
		
			$select->where("to_station_id='$this->to_station_id'");
			
			$shouldCheckWeigt=true;
			
			if($this->carrier!=null){
				
				$select->where("carrier='$this->carrier'");
				
				/*
				$expressCompany=new CExpressCompany();
				
				$companyArray=$expressCompany->getDetail($this->carrier);
				
				if($companyArray[0]['volume_param']==0){
					
					if($this->weight<21){
					
						$select->where("weight='0_21'");
					
					}else{
					
						$select->where("weight='21+'");
					
					}
					
					$shouldCheckWeigt=false;
					
				}*/
				
			}
			
			$select->where("type='$this->type'");
			
			$select->joinLeft('express_companies',"$this->_name.carrier=express_companies.companyid",'*');
		
			$select->joinLeft(R_PRE.'locationfls AS A',"$this->_name.to_station_id=A.flid",'A.en_flname AS to_location');

			$select->joinLeft(R_PRE.'locationfls AS B',"$this->_name.from_station_id=B.flid",'B.en_flname AS from_location');
			
			$select->order('weight ASC');
			
			$select->order('is_recommended DESC');
			
			//$select->order('fuel_surcharge ASC');

			$expressArray=$db->fetchAll($select);
			
			if($shouldCheckWeigt){
				
				$range=array();
				
				foreach($expressArray as $value){
					
					if(!in_array($value['weight'],$range)){
					
						$range[]=$value['weight'];
						
					}
					
				}
				
				$expressArray2=array();
				
				$fixweight=$this->weight;
				
				foreach ($expressArray as &$value2){
					
					if($this->isThisExpress($value2['volume_param'],$value2['weight'],$fixweight,$this->volume,$range)){
						
						$value2['real_weight']=$this->get_weight();

						$expressArray2[]=$value2;
						
					}
					
				}

				return $expressArray2;
				
			}else{
				
				return $expressArray;
				
			}

		}else{
			
			return null;
			
		}
		
	}
	
	public function searchDomestic(){
		
		if($this->from&&$this->currency&&$this->type!=null&&$this->weight){
			
			$db=$this->getAdapter();
			
			$select = $db->select();
			
			$select->from('domestic_express','*');
			
			$select->where("from_station_$this->lang='$this->from'");
			
			if($this->volume==null){
			
				if($this->weight<21){
					
					$select->where("weight='0_21'");
					
				}else{
					
					$select->where("weight='21+'");
					
				}
			
			}else{
				
				if($this->volume>$this->weight){
					
					$this->weight=$this->volume;
					
				}
				
				if($this->weight<21){
					
					$select->where("weight='0_21'");
					
				}else{
					
					$select->where("weight='21+'");
					
				}				
				
			}
			
			$select->where("type='$this->type'");

			return $db->fetchAll($select);

		}else{
			
			return null;
			
		}
		
	}
	
	public function searchD(){
		
		$db=$this->getAdapter();
			
		$select = $db->select();
		
		$select->from('domestic_express','*');
		
		if($this->from!=null&&$this->from!=''){
			
			$select->where("from_station_$this->lang='$this->from'");
			
		}
		
		if($this->weight!=null&&$this->weight!=''){
			
			$select->where("weight='$this->weight'");
			
		}
		
		if($this->type!=null&&$this->type!=''){
			
			$select->where("type='$this->type'");
			
		}

		$select->order("from_station_en ASC");
		
		return $db->fetchAll($select);
		
	}
	
	
	public function searchI(){
		
		$db=$this->getAdapter();
			
		$select = $db->select();
		
		$select->from('international_express','*');

		if($this->to_station_id!=null&&$this->to_station_id!='0'){
			
			$select->where("to_station_id='$this->to_station_id'");
			
		}
		
		if($this->weight!=null&&$this->weight!=''){
			
			$select->where("weight='$this->weight'");
			
		}
		
		if($this->type!=null&&$this->type!=''){
			
			$select->where("type='$this->type'");
			
		}
		
		if($this->carrier!=null&&$this->carrier!='0'){
			
			$select->where("carrier='$this->carrier'");
			
		}
		
		$select->joinLeft('express_companies',"$this->_name.carrier=express_companies.companyid",'*');
		
		$select->joinLeft(R_PRE.'locationfls AS A',"$this->_name.to_station_id=A.flid",'A.en_flname AS to_location');

		$select->joinLeft(R_PRE.'locationfls AS B',"$this->_name.from_station_id=B.flid",'B.en_flname AS from_location');
		
		$select->order("to_station_id ASC");
		
		return $db->fetchAll($select);
		
	}
	
	public function isThisExpress($volume_param,$standardWeight,$currentWeight,$volume,$range){
		
		if($volume_param!='0'){
						
			$volume=$volume/$volume_param;
			
			if($volume>$currentWeight){
				
				$currentWeight=$volume;
				
			}

		}

		$this->setWeight($currentWeight);
		
		$index=array_search($standardWeight,$range);
			
		if($range[$index+1]!=''){
			
			if($currentWeight>$standardWeight&&$range[$index+1]>=$currentWeight){
				
				return true;
				
			}else{
				
				return false;
				
			}
			
		}else{
			
			if($currentWeight>$standardWeight){
				
				return true;
				
			}else{
				
				return false;
				
			}
			
		}
    	
	}
	
}